// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
import Debug_hierachyViewer_singleLabel from './Debug_hierachyViewer_singleLabel';
const {ccclass, property} = cc._decorator;

@ccclass
export default class Debug_hierachyViewer extends cc.Component {
    readonly fontSizeMap: number[] = [40, 36, 32, 28, 24, 20, 16];

    @property(cc.Node)
    labelPrototype: cc.Node = null;

    @property(cc.Node)
    ViewerWindow: cc.Node = null;

    @property(cc.Node)
    contentNode: cc.Node = null;

    AdamNode: cc.Node = null;
    currentLabelID: number = 0;
    currentLevel: number = 0;

    ignoreList: string[] = [];

    updating: boolean = false;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {
        let parentTrans: cc.Node = this.node.parent;
        while (parentTrans.name != 'Canvas' && parentTrans.parent != null) {
            parentTrans = parentTrans.parent;
        }
        this.AdamNode = parentTrans;
        this.updateHierachy();
    }

    updateHierachy() {
        if (!this.updating) {
            this.updating = true;
            this.currentLabelID = 0;
            this.currentLevel = 0;
            this.checkSingleNode(this.AdamNode);

            while (this.currentLabelID < this.contentNode.childrenCount) {
                this.contentNode.children[this.currentLabelID].destroy();
                this.contentNode.children[this.currentLabelID].parent = null;
            }

            /*
            for (let i = 0; i < this.contentNode.childrenCount; i++){
                let singleNodeScr = this.contentNode.children[i].getComponent(Debug_hierachyViewer_singleLabel);
                let fontSize = singleNodeScr.targetLevel > this.fontSizeMap.length ? this.fontSizeMap[this.fontSizeMap.length - 1] : this.fontSizeMap[singleNodeScr.targetLevel];
                singleNodeScr.getComponent(cc.Label).fontSize = fontSize;
            }*/
            this.updating = false;
        }
    }

    checkSingleNode(targetNode: cc.Node) {
        if (targetNode.getComponent(Debug_hierachyViewer) == null) {
            //cc.log(this.currentLabelID + ": " + targetNode.name + '. Label count: ' + this.contentNode.childrenCount);

            let singleNodeScr: Debug_hierachyViewer_singleLabel = null;
            if (this.contentNode.childrenCount > this.currentLabelID) {
                //还有足够的标签
                singleNodeScr = this.contentNode.children[this.currentLabelID].getComponent(Debug_hierachyViewer_singleLabel);
            } else {
                //标签不够了，新建标签
                let temp = cc.instantiate(this.labelPrototype);
                temp.parent = this.contentNode;
                temp.name = this.contentNode.childrenCount.toString();
                singleNodeScr = temp.getComponent(Debug_hierachyViewer_singleLabel);
                singleNodeScr.viewerManager = this;
            }
            singleNodeScr.targetNode = targetNode;
            singleNodeScr.targetLevel = this.currentLevel;
            singleNodeScr.showLabel(targetNode.childrenCount > 0);
            let event_0 = singleNodeScr.fontText.getComponent(cc.Button).clickEvents[0];
            event_0.target = this.node;
            event_0.component = 'Debug_hierachyViewer';
            event_0.handler = 'showOrHideChildren';

            this.contentNode.children[this.currentLabelID].active = true;

            this.currentLabelID++;

            if (this.needToBeIgnored(targetNode.uuid) < 0) {
                this.currentLevel++;
                for (let i = 0; i < targetNode.childrenCount; i++) {
                    this.checkSingleNode(targetNode.children[i]);
                }
                this.currentLevel--;
            }
        }
    }

    needToBeIgnored(targetUUID: string):number {
        for (let i = 0; i < this.ignoreList.length; i++){
            if (this.ignoreList[i] == targetUUID) {
                return i;
            }
        }
        return -1;
    }

    showOrHideViewerWindow() {
        this.ViewerWindow.active = !this.ViewerWindow.active;
        this.updateHierachy();
    }

    showOrHideChildren(event, eventType) {
        cc.log(event.target.parent.name + " " + eventType);
        let ignoreID = this.needToBeIgnored(eventType);
        if (ignoreID >=0 ) {
            //remove this from list
            this.ignoreList.splice(ignoreID, 1);
        } else {
            this.ignoreList.push(eventType);
        }
        this.updateHierachy();
    }
}
